/**
 * Dale Noh, Youngmin Lee, Hyunjoon Lee
 * CSE 481H - Accessibility Capstone
 * Bpass
 * 
 * This class is for 
 */

package com.cse.bpass;

import java.io.File;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;

public class BPassActivity extends Activity implements OnTouchListener, TextToSpeech.OnInitListener{
	protected TextToSpeech tts;	
	private Button[] buttons;
	private final int START = 0;
	private final int SETTING = 1;
	private final int INSTRUCTION = 2;
	private long lastTouch = -1;
	private boolean start;
	private String intro = "There are three buttons on menu. Start, Setting, and Instruction. Search the button " +
			"by tapping the screen. It will tell you which button you touched. If you want to press the button, double" +
			"tap it.";
	
    /** 
     * Called when the activity is first created and initialize.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Create TextToSpeech
        tts = new TextToSpeech(this,this);
        tts.setLanguage(Locale.US);

        start = new File("/sdcard/password.txt").exists();
        
        //Initialize Buttons
        buttons = new Button[3];
        findButtonViews();
        setListeners();
        
    }
    
    /**
     * 
     */
	public void onInit(int status) {
		tts.speak("Welcome to B Pass", TextToSpeech.QUEUE_ADD, null);
		tts.speak(intro, TextToSpeech.QUEUE_ADD, null);
	}
	
	/**
	 * Store each buttons in an array.
	 */
	private void findButtonViews(){
		Display dis = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		int height = dis.getHeight();
		
		buttons[0] = (Button)findViewById(R.id.Start_Button);
		buttons[1] = (Button)findViewById(R.id.Setting_Button);
		buttons[2] = (Button)findViewById(R.id.Instr_Button);
		
		buttons[0].setHeight(height/3);
		buttons[1].setHeight(height/3);
		buttons[2].setHeight(height/3);
	}
	
	/**
	 * 
	 */
	private void setListeners(){
		for(int i = 0; i < buttons.length; i++){
			buttons[i].setOnTouchListener(this);
		}
	}
	
	/**
	 * 
	 * @param selected button id
	 */
	private void startNewActivity(int selected){
		Intent intent = new Intent();
		switch(selected){
		case START:
			intent.setClass(this, Start.class);
			startActivityForResult(intent, 0);
			break;
		case SETTING:
			intent.setClass(this, Setting.class);
			startActivityForResult(intent, 0);
			break;
		
		case INSTRUCTION:
			intent.setClass(this, Instruction.class);
			startActivityForResult(intent, 0);
			break;
		}
	}
	
    @Override
    public void onDestroy() {
        tts.stop();
        tts.shutdown();

        super.onDestroy();
    }
    
	public boolean onTouch(View v, MotionEvent event) {
		start = new File("/sdcard/password.txt").exists();
		if(event.getAction()==MotionEvent.ACTION_UP){
			long currentTime = System.currentTimeMillis();
			if(currentTime - lastTouch < 250){
				lastTouch = -1;				
				switch(v.getId()){
				case R.id.Start_Button:
					if(start){
						startNewActivity(START);
					}else{
						tts.speak("You need to set up the passcode first", TextToSpeech.QUEUE_FLUSH, null);
					}
					break;
				case R.id.Setting_Button:					
					startNewActivity(SETTING);
					break;	
				case R.id.Instr_Button:
					startNewActivity(INSTRUCTION);
					break;
				}
				
			}else{
				if(v.getId() == R.id.Start_Button){
					tts.speak("This is a start button", TextToSpeech.QUEUE_FLUSH, null);
				}else if(v.getId() == R.id.Setting_Button){
					tts.speak("This is a setting button", TextToSpeech.QUEUE_FLUSH, null);
				}else{
					tts.speak("This is a instruction button", TextToSpeech.QUEUE_FLUSH, null);
				}
				lastTouch = currentTime;
			}
		}
		return true;
	}
}